#ifndef UNET_UDP_H
#define UNET_UDP_H

#include <stdint.h>
#include <netinet/in.h>         /* struct in_addr */
#include <netinet/udp.h>

#include "list.h"
#include "rpc.h"
#include "ip.h"
#include "misc.h"
#include <semaphore.h>
#include <pthread.h>
#include <errno.h>


struct list_head conn_list;
pthread_mutex_t conn_list_mutex;

typedef struct
{	// whatever UDP module needs...
        struct in_addr local_addr;
        uint16_t remote_port;
        struct list_head pack_list;   //llista dels paquets de la connexio
        sem_t sem_recv;
	pthread_mutex_t mutex;
	struct list_head connection;

} *unet_udp_connection_t;

struct unet_udp_packet
{
	struct udphdr header;
	uint8_t *payload;
	struct list_head packet;
};


extern unet_udp_connection_t unet_udp_connect (struct in_addr dest_addr, uint16_t src_port, uint16_t dst_port);
extern void unet_udp_conn_close (unet_udp_connection_t state);

extern size_t unet_udp_recv (unet_udp_connection_t state, void *buf, size_t n);
extern void unet_udp_send (unet_udp_connection_t state, const void *buf, size_t n);
extern void unet_udp_incoming (struct in_addr local_ip, const void *buf, size_t n);

void unet_udp_init();

unet_udp_connection_t * list_head_to_udp_connection(struct list_head *l);
struct unet_udp_packet * list_head_to_udp_packet(struct list_head *l);
#endif
